package com.wuxinwudai.android.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * AppUtils 类为应用工具类
 * @author 吾心无待 于 2016年6月4日
 */
public class AppUtils {
    /**
     * 获取包信息
     * @param context 上下文对象
     * @return 包信息
     */
    public static PackageInfo getPackageInfo(Context context) {
        PackageInfo info = null;
        try {
            String packageName = context.getPackageName();
            info = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 获取包名
     * @param context 上下文对象
     * @return 包名
     */
    public static String getPackageName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取当前应用的名称
     * @param context 上下文对象
     * @return 当前应用的名称
     */
    public static String getApplicationName(Context context){
        ApplicationInfo applicationInfo = null;
        PackageManager packageManager = null;
        try {
            packageManager = context.getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(getPackageName(context),0);
        }catch (PackageManager.NameNotFoundException e){
            return "";
        }
        return (String) packageManager.getApplicationLabel(applicationInfo);
    }
}
